package com.mj.mes.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;

/**
 * Created by qzy on 2017/5/14.
 */
@Configuration
public class RabbitmqServer {
    @Bean
    RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    @Bean
    public RabbitMessagingTemplate rabbitMessagingTemplate(RabbitTemplate rabbitTemplate) {
        RabbitMessagingTemplate rabbitMessagingTemplate = new RabbitMessagingTemplate();
//        rabbitMessagingTemplate.setMessageConverter(jackson2Converter());
        rabbitMessagingTemplate.setRabbitTemplate(rabbitTemplate);
        return rabbitMessagingTemplate;
    }
    @Bean
    public Jackson2JsonMessageConverter jackson2Converter() {
        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
        return converter;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }

    @Bean
    TopicExchange contractTopicExchange(RabbitAdmin rabbitAdmin) {
        TopicExchange TopicExchange = new TopicExchange(RabbitmqConfig.MJ_MES_EXCHANGE);
        rabbitAdmin.declareExchange(TopicExchange);
        return TopicExchange;
    }
    
    @Bean
    TopicExchange simulatorExchange(RabbitAdmin rabbitAdmin) {
        //Topic  Durable
        TopicExchange TopicExchange = new TopicExchange(RabbitmqConfig.SIMULATOR_EXCHANGE);
        rabbitAdmin.declareExchange(TopicExchange);
        return TopicExchange;
    }

    @Bean
    public Queue simulatorQueue(RabbitAdmin rabbitAdmin) {
        Queue queue = new Queue("DEVICE_SIMULATOR_QUEUE");
        rabbitAdmin.declareQueue(queue);
        return queue;
    }
//    @Bean
//    public Queue queueapp(RabbitAdmin rabbitAdmin) {
//        Queue queue = new Queue(RabbitmqQueue.DATONG_QUEUE);
//        rabbitAdmin.declareQueue(queue);
//        return queue;
//    }
    @Bean
    Binding bindingSimulatorExchange(Queue simulatorQueue, TopicExchange simulatorExchange, RabbitAdmin rabbitAdmin) {
        Binding binding = BindingBuilder.bind(simulatorQueue)
                .to(simulatorExchange)
                .with("simulator.#");
        rabbitAdmin.declareBinding(binding);
        return binding;
    }
}
